<?php
/**
 * 物件ソートクラス<br />
 *
 * @package apamanshop
 * @author SS永田
 * @copyright コピーライト(要確認)
 */
class Rstobi_Model_Sort extends Apm_Model_Base
{
	/*
	 * 物件ソート設定を取得
	 * @access	public
	 * @return  array
	 */
	public function getSortSetting()
	{
		// ソート項目
		//
		//   DISP_ORDER        ：表示順
		//   DISP_NAME         ：表示名
		//   SORT_COLUMN_COMMON：並び替えカラム名（共通用）
		//   SORT_COLUMN_TENPO ：並び替えカラム名（OEM用）
		//   SORT_ORDER        ：並び替え順
		//   SORT_CD           ：VALUE値
		//   DEFAULT_SORT_FLG  ：初期並び替え項目

		// TODO ソート項目の変更を行う

		$SORTITEM_SETTING = array();
		$SORTITEM_SETTING['TINRYO_ASC']     = array("DISP_ORDER"         => "1",
													"DISP_NAME"          => "賃料(管理費込み)の安い順",
													"SORT_COLUMN_COMMON" => "T03_YACHIN_KANRI_KYOEKI_SUM",
													"SORT_COLUMN_TENPO"  => "T03_YACHIN_KANRI_KYOEKI_SUM",
													"SORT_COLUMN_FOR_FREE"  => "YACHIN",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"             => "01",
													"DEFAULT_SORT_FLG"   => '1');

		$SORTITEM_SETTING['TINRYO_DESC']    = array("DISP_ORDER"         => "2",
													"DISP_NAME"          => "賃料(管理費込み)の高い順",
													"SORT_COLUMN_COMMON" => "T03_YACHIN_KANRI_KYOEKI_SUM",
													"SORT_COLUMN_TENPO"  => "T03_YACHIN_KANRI_KYOEKI_SUM",
													"SORT_COLUMN_FOR_FREE"  => "YACHIN",
													"SORT_ORDER"         => "DESC",
													"SORT_CD"            => "02",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['MEYASU_ASC']     = array("DISP_ORDER"         => "3",
													"DISP_NAME"          => "めやす賃料の安い順",
													// 2010/12/15 集約表示戻し対応
//													"SORT_COLUMN_COMMON" => "NVL(T03_MEYASU_CHINRYO,9999999)",
													"SORT_COLUMN_COMMON" => " (CASE WHEN NVL(T03_TEN_MEYASU_CHINRYO,0) != 0 THEN T03_TEN_MEYASU_CHINRYO ELSE DECODE(NVL(T03_MEYASU_CHINRYO,0),0,9999999,T03_MEYASU_CHINRYO) END) ",
													"SORT_COLUMN_TENPO"  => "DECODE(NVL(T03_TEN_MEYASU_CHINRYO,0),0,9999999,T03_TEN_MEYASU_CHINRYO)",
													"SORT_COLUMN_FOR_FREE"  => "MEYASU_CHINRYO",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"            => "03",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['MEYASU_DESC']    = array("DISP_ORDER"         =>  "4",
													"DISP_NAME"          => "めやす賃料の高い順",
													// 2010/12/15 集約表示戻し対応
//													"SORT_COLUMN_COMMON" => "NVL(T03_MEYASU_CHINRYO,0)",
													"SORT_COLUMN_COMMON" => " (CASE WHEN NVL(T03_TEN_MEYASU_CHINRYO,0) != 0 THEN T03_TEN_MEYASU_CHINRYO ELSE NVL(T03_MEYASU_CHINRYO,0) END) ",
													"SORT_COLUMN_TENPO"  => "NVL(T03_TEN_MEYASU_CHINRYO,0)",
													"SORT_COLUMN_FOR_FREE"  => "MEYASU_CHINRYO",
													"SORT_ORDER"         => "DESC",
													"SORT_CD"            => "04",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['JYUSYO_ASC']	    = array("DISP_ORDER"         =>  "5",
													"DISP_NAME"          => "住所順(昇順)",
													"SORT_COLUMN_COMMON" => "T03_JYUUSHO_TEXT",
													"SORT_COLUMN_TENPO"  => "T03_JYUUSHO_TEXT",
													"SORT_COLUMN_FOR_FREE"  => "JYUUSHO",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"            => "05",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['JYUSYO_DESC']    = array("DISP_ORDER"         =>  "6",
													"DISP_NAME"          => "住所順(降順)",
													"SORT_COLUMN_COMMON" => "T03_JYUUSHO_TEXT",
													"SORT_COLUMN_TENPO"  => "T03_JYUUSHO_TEXT",
													"SORT_COLUMN_FOR_FREE"  => "JYUUSHO",
													"SORT_ORDER"         => "DESC",
													"SORT_CD"            => "06",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['MENSEKI_ASC']    = array("DISP_ORDER"         =>  "7",
													"DISP_NAME"          => "専有面積の広い順",
													"SORT_COLUMN_COMMON" => "T03_SENYUU_MENSEKI",
													"SORT_COLUMN_TENPO"  => "T03_SENYUU_MENSEKI",
													"SORT_COLUMN_FOR_FREE"  => "MENSEKI",
													"SORT_ORDER"         => "DESC",
													"SORT_CD"            => "07",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['MENSEKI_DESC']   = array("DISP_ORDER"         =>  "8",
													"DISP_NAME"          => "専有面積の狭い順",
													"SORT_COLUMN_COMMON" => "T03_SENYUU_MENSEKI",
													"SORT_COLUMN_TENPO"  => "T03_SENYUU_MENSEKI",
													"SORT_COLUMN_FOR_FREE"  => "MENSEKI",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"            => "08",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['TIKUNENSU_ASC']  = array("DISP_ORDER"         =>  "9",
													"DISP_NAME"          => "築年数の新しい順",
													"SORT_COLUMN_COMMON" => "T03_TIKU_NENSU",
													"SORT_COLUMN_TENPO"  => "T03_TIKU_NENSU",
													"SORT_COLUMN_FOR_FREE"  => "TIKU_NENSU",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"            => "09",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['TIKUNENSU_DESC'] = array("DISP_ORDER"         => "10",
													"DISP_NAME"          => "築年数の古い順",
													"SORT_COLUMN_COMMON" => "T03_TIKU_NENSU",
													"SORT_COLUMN_TENPO"  => "T03_TIKU_NENSU",
													"SORT_COLUMN_FOR_FREE"  => "TIKU_NENSU",
													"SORT_ORDER"         => "DESC",
													"SORT_CD"            => "10",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['MADORI_ASC']     = array("DISP_ORDER"         => "11",
													"DISP_NAME"          => "間取り(昇順)",
													"SORT_COLUMN_COMMON" => "T03_MADORI_CD",
													"SORT_COLUMN_TENPO"  => "T03_MADORI_CD",
													"SORT_COLUMN_FOR_FREE"  => "MADORI",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"            => "11",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['MADORI_DESC']    = array("DISP_ORDER"         => "12",
													"DISP_NAME"          => "間取り(降順)",
													"SORT_COLUMN_COMMON" => "T03_MADORI_CD",
													"SORT_COLUMN_TENPO"  => "T03_MADORI_CD",
													"SORT_COLUMN_FOR_FREE"  => "MADORI",
													"SORT_ORDER"         => "DESC",
													"SORT_CD"            => "12",
													"DEFAULT_SORT_FLG"   => '0');

		$SORTITEM_SETTING['TATEMONO_TYPE_ASC']     = array("DISP_ORDER"         => "13",
													"DISP_NAME"          => "建物種別(昇順)",
													"SORT_COLUMN_COMMON" => "T03_TATEMONO_TYPE",
													"SORT_COLUMN_TENPO"  => "T03_TATEMONO_TYPE",
													"SORT_COLUMN_FOR_FREE"  => "TATEMONO_TYPE",
													"SORT_ORDER"         => "ASC",
													"SORT_CD"            => "13",
													"DEFAULT_SORT_FLG"   => '0');

		return $SORTITEM_SETTING;
	}

	/*
	 * デフォルトの並び替え設定を取得
	 * @access	public
	 * @param   array $params
	 * @return  array $defaultSort
	 */
	public function getDefaultSort($params)
	{
		// インスタンス生成
		$this->_model_common = new Rstobi_Model_Common();

		$defaultSort = array();
		$common = '_COMMON';

		// OEMの場合は、店別のカラムでソート
		if($this->_model_common->isOem($params)) { $common = '_TENPO'; }

		//並び替え設定取得
		$sortsetting = $this->getSortSetting();
		foreach( $sortsetting as $key => $val )
		{
			// デフォルトフラグが立っている設定を取得
			if($val['DEFAULT_SORT_FLG'] == '1')
			{
				$defaultSort['SORT_COLUMN'] = $val['SORT_COLUMN'.$common];
				$defaultSort['SORT_ORDER']  = $val['SORT_ORDER'];
				$defaultSort['SORT_COLUMN_FOR_FREE'] = $val['SORT_COLUMN_FOR_FREE'];
				break;
			}
		}

		return $defaultSort;
	}


	/*
	 * 並び替え設定を取得
	 * @access	public
	 * @param   array $params
	 * @return  array $sortData
	 */
	public function getSortData($params)
	{
		// インスタンス生成
		$this->_model_common = new Rstobi_Model_Common();

		$sortData = array();
		$common = '_COMMON';

		// OEMの場合は、店別のカラムでソート
//		if($this->_model_common->isOem($params)) { $common = '_TENPO'; }

		if((array_key_exists('SortCd', $params)) == true && (is_null($params['SortCd']) == false ) && !empty($params['SortCd']))
		{
			//並び替え設定取得
			$sortsetting = $this->getSortSetting();
			foreach( $sortsetting as $key => $val )
			{
				if($val['SORT_CD'] == $params['SortCd'])
				{
					$sortData['SORT_COLUMN'] = $val['SORT_COLUMN'.$common];
					$sortData['SORT_ORDER']  = $val['SORT_ORDER'];
					$sortData['SORT_COLUMN_FOR_FREE'] = $val['SORT_COLUMN_FOR_FREE'];
					break;
				}
			}
		}
		// 並び替え情報がない場合は、デフォルト設定を使用
		else { $sortData = $this->getDefaultSort($params); }

		return $sortData;
	}

	/*
	 * 並び替え情報を画面表示用に成形して取得
	 * @access	public
	 * @param   array $params
	 * @return  array $dispSortData
	 */
	public function getDispSortData($params)
	{
		$dispSortData = array();

		// 並び替えデータ取得
		$sortsetting = $this->getSortSetting();

		// 成形
		foreach( $sortsetting as $key => $val )
		{
			$dispSortData[$val['DISP_ORDER']]['DISP_NAME'] = $val['DISP_NAME']; // 表示文言
			$dispSortData[$val['DISP_ORDER']]['SORT_CD']   = $val['SORT_CD'];   //並び替えCD

			if($params['SortCd'] == $val['SORT_CD'] || (empty($params['SortCd']) && $val['DEFAULT_SORT_FLG'] == '1')){
				$dispSortData[$val['DISP_ORDER']]['SELECTED'] = "selected";
			}
			else
			{
				$dispSortData[$val['DISP_ORDER']]['SELECTED'] = "";
			}
		}

		return $dispSortData;
	}
}
?>